<template>
  <div class="contract">
    <!-- 合同管理 -->
    <el-form :inline="true"
             :model="dataForm"
             @keyup.enter.native="getDataList()">
      <el-form-item prop="companyName">
        <el-input v-model="dataForm.contractName"
                  style="width: 330px"
                  placeholder="请输入编号、合同名称、创建者"
                  clearable></el-input>
      </el-form-item>
      <el-form-item>
        <!-- 搜素按钮 -->
        <el-button type="primary"
                   icon="el-icon-search"
                   @click="search()"></el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格开始 -->
    <el-table :data="tableData"
              border
              v-loading="dataListLoading"
              @selection-change="selectionChangeHandle"
              style="width: 100%">
      <!-- <el-table-column type="selection"
                       header-align="center"
                       align="center"
                       width="50">
      </el-table-column> -->
      <el-table-column prop="id"
                       fixed
                       header-align="center"
                       align="center"
                       width="72"
                       label="ID">
      </el-table-column>
      <el-table-column prop="contractNum"
                       header-align="center"
                       align="center"
                       width="207"
                       label="编号">
      </el-table-column>
      <el-table-column prop="contractName"
                       header-align="center"
                       align="center"
                       label="合同名称">
      </el-table-column>
      <el-table-column prop="createUser"
                       header-align="center"
                       align="center"
                       width="207"
                       label="创建者">
      </el-table-column>
      <el-table-column label="操作"
                       align="center"
                       fixed="right"
                       width="221">
        <template slot-scope="scope">
          <el-button type="text"
                     size="small"
                     @click="inquire(scope.row.id)">查询</el-button>
          <el-button @click="update(scope.row.id)"
                     type="text"
                     size="small">修改</el-button>
          <el-button type="text"
                     size="small"
                     @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <!-- 分页前说明 -->
      <div class="instructions">显示第{{tableData.length>0?((pageIndex-1)*pageSize+1):0}}到第{{(pageIndex-1)*pageSize+tableData.length}}条数据，总共{{totalPage}}条数据
        <!-- ，每页显示 -->
        <!-- 条数据 -->
      </div>
      <!-- 分页 -->
      <el-pagination @size-change="sizeChangeHandle"
                     @current-change="currentChangeHandle"
                     :current-page="pageIndex"
                     :page-sizes="[10, 20, 50]"
                     :page-size="pageSize"
                     :total="totalPage"
                     layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
      <!-- 分页2 -->
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <addContract v-if="addOrUpdateVisible"
                 ref="addOrUpdate"
                 @refreshDataList="getDataList"></addContract>
    <!-- 查询 -->
    <lookContract v-if="lookContractVisible"
                  ref="lookContract"></lookContract>
  </div>
</template>

<script>
// import ueditor from 'ueditor'
import addContract from './add/addContract'
import lookContract from './add/lookContract'
export default {
  data () {
    return {
      dataForm: {
        contractName: ''
      },
      // headers: {
      //   token: ''
      // },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      // 查询显示隐藏
      lookContractVisible: false,
      // 时间区间,开始，结束时间
      // timeInterval: '',
      // startTime: '',
      // endTime: '',
      // 假数据
      tableData: [],
      // 上传文件
      fileList: [],
      // 上传地址
      // action: '',
      workerId: ''
    }
  },
  components: {
    addContract,
    lookContract
  },
  activated () {
    // 上传地址
    // this.action = this.$baseUrl + '/sys/tax/import'
    // this.headers.token = this.$cookie.get('token')
    this.getDataList()
  },
  watch: {
    // timeInterval () {
    //   try {
    //     this.startTime = this.timeInterval[0]
    //     this.endTime = this.timeInterval[1]
    //   } catch (error) {
    //     this.startTime = ''
    //     this.endTime = ''
    //   }
    //   this.getDataList()
    // }
  },
  methods: {
    // 获取数据列表
    getDataList () {
      // this.tableData = [{
      //   id: '1',
      //   companyName: '1',
      //   personName: '我是合同名称',
      //   bankName: '<p>合同内容</p>'
      // }]
      // this.totalPage = 1
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/sys/contract/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'contractName': this.dataForm.contractName
        })
      }).then(({ data }) => {
        // console.log(data)
        this.dataListLoading = false
        this.tableData = data.page.list
        this.totalPage = data.page.totalCount
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增
    addOrUpdateHandle (id) {
      // 显示
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.mode = 'add'
        if (sessionStorage.getItem('userRoleId') === '2') {
          this.workerId = this.$store.state.user.id
        }
        let createUser = this.$store.state.user.name
        this.$refs.addOrUpdate.dataForm = {
          cid: this.workerId,
          contractNum: '',
          contractName: '',
          contractText: '',
          createUser: createUser
        }
        // this.$refs.addOrUpdate.ue.getContent() = ''
        this.$refs.addOrUpdate.addInit()
      })
    },
    // 查询
    inquire (id) {
      // 显示
      this.lookContractVisible = true
      this.$nextTick(() => {
        this.$refs.lookContract.visible = true
        this.$refs.lookContract.init(id)
      })
    },
    // 编辑
    update (id) {
      // 显示
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.mode = 'edit'
        this.$refs.addOrUpdate.visible = true
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle (id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定删除操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/sys/contract/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.pageIndex = 1
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => { })
    },
    // 接下来
    // 按钮搜索
    search () {
      this.pageIndex = 1
      this.getDataList()
    }
  }
}
</script>

<style lang="scss" scoped>
.contract {
  .pagination {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .instructions {
      height: 19px;
      font-size: 14px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      line-height: 19px;
      color: #333333;
    }
  } // 导入
  button.el-button.btnColor.el-button--info.el-button--medium {
    position: relative;
    overflow: hidden;
  }
  .file {
    display: inline;
    position: relative;
    overflow: hidden;
    .fileBtn {
      opacity: 0;
      width: 100%;
      height: 100%;
      position: absolute;
      top: 0;
      left: 0;
    }
    .upload-demo {
      display: inherit;
    }
  }
}
</style>